<template>
  <div class="flow-box w-card">
    <div class="content-box">
      <div class="title-box">
        <span>线下代充记录</span>
      </div>
      <div class="search-box w-flex w-f-a-c w-mt-20 w-gap-10">
        <div class="w-search-item">
          <span>订单ID：</span>
          <el-input v-model="searchForm.orderId" clearable placeholder="请输入订单ID" />
        </div>
        <div class="w-search-item">
          <span>代充商户：</span>
          <el-input v-model="searchForm.toShopName" clearable placeholder="请输入代充商户" />
        </div>
        <div class="w-search-item">
          <span>生成时间：</span>
          <el-date-picker v-model="searchForm.date" type="datetimerange" range-separator="至" start-placeholder="开始时间"
            end-placeholder="结束时间" />
        </div>
        <el-button type="primary" @click="searchHandle">查询</el-button>
        <el-button style="margin-left:0;" @click="resetHandle">重置</el-button>
      </div>
      <div class="table-box">
        <el-table :height="tableHeight" :header-cell-style="{background:'#d6dee5',color:'#000000',fontSize:'14px'}"
          :cell-style="{fontSize:'14px',color:'#7b7b7d'}" :data="tableData" stripe>
          <el-table-column type="index" width="55" label="序号" align="center" />
          <el-table-column prop="orderId" label="订单ID" align="center">
            <template v-slot="scope">
              {{ scope.row.orderId || '' }}
            </template>
          </el-table-column>
          <el-table-column prop="toShopIdName" label="代充商户" align="center">
            <template v-slot="scope">
              {{ scope.row.toShopIdName || '' }}
            </template>
          </el-table-column>
          <el-table-column prop="rechFee" label="代充金额(元)" align="center">
            <template v-slot="scope">
              {{ numFormat(scope.row.rechFee/100) || '' }}
            </template>
          </el-table-column>
          <el-table-column prop="remark" label="备注信息" align="center">
            <template v-slot="scope">
              {{ scope.row.remark || '' }}
            </template>
          </el-table-column>
          <el-table-column prop="userName" label="操作人" align="center">
            <template v-slot="scope">
              {{ scope.row.userName || '' }}
            </template>
          </el-table-column>
          <el-table-column prop="createTime" label="生成时间" align="center">
            <template v-slot="scope">
              {{ scope.row.createTime ? getDateTime(scope.row.createTime)[6] : '' }}
            </template>
          </el-table-column>
        </el-table>
        <div class="table-footer">
          <el-pagination v-model:currentPage="page" v-model:page-size="size" :page-sizes="[10, 20, 50, 100]" small
            :background="true" layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange"
            @current-change="handleCurrentChange" />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { onMounted, reactive, toRefs } from 'vue'
import { getWindowWidthAndHeight, warningDeal, getDateTime, numFormat } from '@/utils/utils'
import api from '../api'
export default {
  setup() {
    const state = reactive({
      tableHeight: 0,
      page: 1,
      size: 10,
      total: 0,
      searchForm: {},
      tableData: [],
    })

    function getTableData() {
      const obj = {
        page: state.page,
        size: state.size,
        payType: 16,
        orderBy: 'id desc',
        ...state.searchForm,
        startTime: (state.searchForm.date && state.searchForm.date.length > 0) ? state.searchForm.date[0].getTime() : null,
        endTime: (state.searchForm.date && state.searchForm.date.length > 0) ? state.searchForm.date[1].getTime() : null,
      }
      api.accountChargeFlow(obj).then(res => {
        if (res.code == '200') {
          state.tableData = res.data.list
          state.total = res.data.total
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function searchHandle() {
      state.page = 1
      getTableData()
    }

    function resetHandle() {
      state.searchForm = {}
      searchHandle()
    }

    function handleSizeChange(e) {
      state.size = e
      getTableData()
    }

    function handleCurrentChange(e) {
      state.page = e
      getTableData()
    }

    onMounted(() => {
      let winHeight = getWindowWidthAndHeight()
      state.tableHeight = winHeight.windowHeight - 290
      getTableData()
    })

    return {
      ...toRefs(state),
      handleSizeChange,
      handleCurrentChange,
      getDateTime,
      resetHandle,
      searchHandle,
      numFormat,
    }
  }
}   
</script>

<style lang="scss" scoped>
.flow-box {
  width: 100%;
  height: 100%;
  overflow: hidden;
  .content-box {
    width: 100%;
    height: 100%;
    overflow-y: auto;
    box-sizing: border-box;
    padding: 20px;
    .title-box {
      font-size: 16px;
      font-weight: bold;
    }
    .table-box {
      margin-top: 20px;
      .table-footer {
        margin-top: 20px;
        display: flex;
        justify-content: flex-end;
      }
    }
  }
}
</style>